<template>
  <div class="container">
    <div class="content">
      <div class="school_type">
        <div class="title">办学类型</div>
        <div class="example">
          <span
            v-for="(item, index) in runningSchool"
            :key="index"
            :class="{ active: item.flag }"
            @click="RunningActive(index)"
            >{{ item.name }}</span
          >
        </div>
      </div>
      <div class="school_feature">
        <div class="title">院校特色</div>
        <div class="example">
          <span
            v-for="(item, index) in feature"
            :key="index"
            :class="{ active: item.flag }"
            @click="FeatureActive(index)"
            >{{ item.name }}</span
          >
        </div>
      </div>
      <div class="school_colleage">
        <div class="title">院校类型</div>
        <div class="example">
          <span
            v-for="(item, index) in colleageSchool"
            :key="index"
            :class="{ active: item.flag }"
            @click="ColleageActive(index)"
            >{{ item.name }}
          </span>
        </div>
      </div>
      <div class="computed">
        <div>
          已选
          <span class="num">{{ checkTypes.length }}</span>
          个
          <span
            v-if="checkTypes.length > 0"
            class="jiantou"
            :class="{ rotate: watch, antiRotated: !watch }"
            @click="watch = !watch"
            ><i class="iconfont icon-jiantouxia"></i
          ></span>
        </div>
        <div>
          <button @click="clearChecked">清除院校城市</button>
        </div>
        <div>
          <router-link to="/school_i">
            <button @click="confirmSelection">确定</button>
          </router-link>
        </div>
      </div>
      <div class="watch" v-if="watch">
        <div class="examples">
          <div class="title">
            <span>已选内容</span>
            <span @click="watch = !watch"
              ><i class="iconfont icon-quxiao"></i
            ></span>
          </div>
          <div class="city">
            <div>
              院校类型<span>（{{ checkTypes.length }}）</span>
            </div>
            <div>
              <span v-for="(item, index) in checkTypes" :key="index"
                >{{ item }}
                <div class="mark" @click="cancelChecked(index)">
                  <i class="iconfont icon-quxiao"></i>
                </div>
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "cityComponent",
  data() {
    return {
      // 办学类型
      runningSchool: [
        "办公",
        "民办",
        "中外合作办学",
        "内地与港澳台地区合作办学",
      ],
      // 院校特点
      feature: ["211", "985", "双一流", "强基计划", "双高计划"],
      // 院校类型
      colleageSchool: [
        "综合类",
        "理工类",
        "农林类",
        "医药类",
        "师范类",
        "语言类",
        "财经类",
        "政法类",
        "体育类",
        "艺术类",
        "民族类",
        "军事类",
        "其他",
      ],
      checkTypes: [],
      watch: false,
    };
  },
  methods: {
    RunningActive(index) {
      this.runningSchool[index].flag = !this.runningSchool[index].flag;
      if (this.runningSchool[index].flag) {
        // 如果当前点击的城市被选中，则将其加入到已选择的城市列表中
        this.checkTypes.push(this.runningSchool[index].name);
        console.log(this.checkTypes);
      } else {
        // 如果当前点击的城市被取消选中，则将其从已选择的城市列表中移除
        const typeIndex = this.checkTypes.indexOf(this.runningSchool[index]);
        if (typeIndex > -1) {
          this.checkTypes.splice(typeIndex, 1);
        }
        console.log(this.checkTypes);
      }
    },
    FeatureActive(index) {
      this.feature[index].flag = !this.feature[index].flag;
      if (this.feature[index].flag) {
        // 如果当前点击的城市被选中，则将其加入到已选择的城市列表中
        this.checkTypes.push(this.feature[index].name);
        console.log(this.checkTypes);
      } else {
        // 如果当前点击的城市被取消选中，则将其从已选择的城市列表中移除
        const typeIndex = this.checkTypes.indexOf(this.feature[index].name);
        if (typeIndex > -1) {
          this.checkTypes.splice(typeIndex, 1);
        }
        console.log(this.checkTypes);
      }
    },
    ColleageActive(index) {
      this.colleageSchool[index].flag = !this.colleageSchool[index].flag;
      if (this.colleageSchool[index].flag) {
        // 如果当前点击的城市被选中，则将其加入到已选择的城市列表中
        this.checkTypes.push(this.colleageSchool[index].name);
        console.log(this.checkTypes);
      } else {
        // 如果当前点击的城市被取消选中，则将其从已选择的城市列表中移除
        const typeIndex = this.checkTypes.indexOf(this.colleageSchool[index]);
        if (typeIndex > -1) {
          this.checkTypes.splice(typeIndex, 1);
        }
        console.log(this.checkTypes);
      }
    },
    clearChecked() {
      this.runningSchool.forEach((type) => {
        type.flag = false;
      });
      this.feature.forEach((type) => {
        type.flag = false;
      });
      this.colleageSchool.forEach((type) => {
        type.flag = false;
      });
      this.checkTypes = [];
    },
    confirmSelection() {
      sessionStorage.setItem("checkTypes", JSON.stringify(this.checkTypes));
    },
    cancelChecked(index) {
      const typeName = this.checkTypes[index];
      console.log(typeName);
      this.checkTypes.splice(index, 1);
      this.runningSchool = this.runningSchool.map((type) => {
        if (type.name === typeName) {
          return { ...type, flag: false };
        }
        return type;
      });
      this.feature = this.feature.map((type) => {
        if (type.name === typeName) {
          return { ...type, flag: false };
        }
        return type;
      });
      this.colleageSchool = this.colleageSchool.map((type) => {
        if (type.name === typeName) {
          return { ...type, flag: false };
        }
        return type;
      });
    },
  },
  mounted() {
    this.runningSchool = this.runningSchool.map((item) => {
      return { name: item, flag: false };
    });
    console.log(this.runningSchool);
    this.feature = this.feature.map((item) => {
      return { name: item, flag: false };
    });
    console.log(this.feature);
    this.colleageSchool = this.colleageSchool.map((item) => {
      return { name: item, flag: false };
    });
    console.log(this.colleageSchool);
  },
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
.content {
  width: 100vw;
  height: 564px;
  overflow: auto;
}
.content::-webkit-scrollbar {
  display: none;
}
.school_type {
  font-size: 18px;
  font-weight: 700;
  margin: 0 15px;
  letter-spacing: 1px;
  .example {
    height: 120px;
    span {
      display: inline-block;
      width: 105px;
      height: 40px;
      text-align: center;
      line-height: 40px;
      background-color: rgb(248, 248, 250);
      border-radius: 5px;
      font-size: 14px;
      margin-top: 10px;
      margin-right: 10px;
      cursor: pointer;
      border: 2px solid rgb(248, 248, 250);
    }
    span:nth-child(4) {
      width: 190px;
    }
    // span标签高亮样式
    .active {
      border-color: rgb(0, 119, 255);
      color: rgb(0, 119, 255);
    }
  }
}
.school_feature {
  font-size: 18px;
  font-weight: 700;
  margin: 0 15px;
  letter-spacing: 1px;
  height: 150px;
  .example {
    height: 120px;
    span {
      display: inline-block;
      width: 105px;
      height: 40px;
      text-align: center;
      line-height: 40px;
      background-color: rgb(248, 248, 250);
      border-radius: 5px;
      font-size: 14px;
      margin-top: 10px;
      margin-right: 10px;
      cursor: pointer;
      border: 2px solid rgb(248, 248, 250);
    }
    // span标签高亮样式
    .active {
      border-color: rgb(0, 119, 255);
      color: rgb(0, 119, 255);
    }
  }
  // span标签高亮样式
  .active {
    border-color: rgb(0, 119, 255);
    color: rgb(0, 119, 255);
  }
}
.school_colleage {
  font-size: 18px;
  font-weight: 700;
  margin: 0 15px;
  letter-spacing: 1px;
  height: 255px;
  .example {
    height: 255px;
    span {
      display: inline-block;
      width: 105px;
      height: 40px;
      text-align: center;
      line-height: 40px;
      background-color: rgb(248, 248, 250);
      border-radius: 5px;
      font-size: 14px;
      margin-top: 10px;
      margin-right: 10px;
      cursor: pointer;
      border: 2px solid rgb(248, 248, 250);
    }
    // span标签高亮样式
    .active {
      border-color: rgb(0, 119, 255);
      color: rgb(0, 119, 255);
    }
  }
}
// 共同样式共同管理
// title样式
.title {
  height: 40px;
  line-height: 40px;
}
.computed {
  width: 100vw;
  height: 55px;
  line-height: 55px;
  background-color: white;
  display: flex;
  position: fixed;
  z-index: 4;
  bottom: 0px;
  right: 0;
  font-size: 14px;
}
.computed div:nth-child(1) {
  width: 30%;
  text-align: center;
  .num {
    color: rgb(0, 119, 255);
  }
  .jiantou {
    i {
      font-size: 12px;
    }
  }
}
.computed div:nth-child(2),
.computed div:nth-child(3) {
  width: 35%;
  height: 55px;
  display: flex;
  align-items: center;
  justify-content: center;
  button {
    width: 100px;
    height: 35px;
    line-height: 35px;
    border: none;
    background-color: rgb(248, 248, 248);
    border-radius: 10px;
    color: gray;
    font-size: 13px;
    letter-spacing: 1px;
  }
}
.computed div:nth-child(3) {
  button {
    width: 80px;
    color: white;
    background-color: rgb(37, 82, 245);
  }
}
.watch {
  width: 100vw;
  height: 800px;
  background-color: rgba(0, 0, 0, 0.4);
  display: flex;
  position: fixed;
  z-index: 4;
  bottom: 0;
  right: 0;
  font-size: 14px;
  .examples {
    width: 100vw;
    height: 300px;
    position: fixed;
    bottom: 0px;
    right: 0;
    display: flex;
    flex-direction: column;
    background-color: white;
    .title {
      width: 100vw;
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: center;
      border: 1px solid gainsboro;
    }
    .title span:nth-child(1) {
      display: block;
      width: 310px;
      height: 30px;
      line-height: 30px;
      text-align: left;
      letter-spacing: 1px;
      font-size: 16px;
      font-weight: 700;
    }
    .title span:nth-child(2) {
      display: block;
      width: 30px;
      height: 30px;
      line-height: 30px;
      text-align: center;
      font-size: 18px;
      font-weight: 700;
      cursor: pointer;
      i {
        font-size: 18px;
      }
    }
    .city {
      width: 100vw;
      height: 230px;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
    .city div:nth-child(1) {
      width: 91%;
      height: 30px;
      line-height: 30px;
      margin: 0 15px;
      font-size: 14px;
      font-weight: 700;
      letter-spacing: 1px;
      border-bottom: 1px solid gainsboro;
      span {
        color: rgb(0, 119, 255);
      }
    }
    .city div:nth-child(2) {
      width: 355px;
      height: 210px;
      overflow: auto;
      span {
        background-color: rgb(248, 248, 248);
        display: inline-block;
        box-sizing: border-box;
        // width: 90px;
        padding: 0 15px;
        height: 30px;
        line-height: 30px;
        text-align: center;
        letter-spacing: 2px;
        font-size: 13px;
        font-weight: 700;
        border-radius: 5px;
        margin-left: 13px;
        margin-right: 5px;
        position: relative;
        margin-top: 10px;
        overflow: hidden;
        .mark {
          width: 30px;
          height: 30px;
          line-height: 24px;
          border-radius: 50%;
          background-color: rgb(243, 207, 206);
          color: red;
          position: absolute;
          text-align: left;
          padding-left: 2px;
          right: -27px;
          bottom: -10px;
        }
      }
    }
    .city div:nth-child(2)::-webkit-scrollbar {
      display: none;
    }
    .city::-webkit-scrollbar {
      display: none;
    }
  }
}
</style>
